﻿using System;
using System.IO;
using System.Xml.Serialization;
using System.Web.Script.Serialization;

using Fuse8.ContentManagementFramework.SystemFacade.Extensions;

namespace Fuse8.ContentManagementFramework.Domain
{
    public partial class CmsMediaFile
    {
        public const string FrontEndUrlFormat = "/mediafile/id/{0}{1}";

        [XmlIgnore]
        [ScriptIgnore]
        public bool IsPrimary { get; set; }

        public string Size
        {
            get
            {
                return FileSize.ToFileSize();
            }
        }

        public bool IsImage
        {
            get
            {
                bool result = false;

                if (MimeType != null)
                {
                    result = MimeType.IsImage;
                }

                return result;
            }
        }

        public string ContentType
        {
            get
            {
                string result = string.Empty;

                if (MimeType != null)
                {
                    result = MimeType.ContentType;
                }

                return result;
            }
        }

        public string FileType
        {
            get
            {
                var parts = ContentType.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                return parts.Length > 1 ? parts[1] : string.Empty;
            }
        }

        public string FileExtension
        {
            get
            {
                return Path.GetExtension(LinkedFileName);
            }
        }

        public virtual string FrontEndUrl
        {
            get
            {
                return string.Format(FrontEndUrlFormat, Id, FileExtension);
            }
        }

        public bool BlockFromSearchEngines { get; set; }
    }
}
